package ar.com.juanlopezcervero.flexigrid4j.params;

import java.io.Serializable;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class FlexigridParams implements Serializable {

	private static final long serialVersionUID = -4821235468956157559L;

	public static final String JSON = "json";
	public static final String ORDER_ASC = "asc";
	public static final String ORDER_DESC = "desc";
	
	private String url;
	private String dataType;
	private List<FlexigridColumn> colModel;
	private List<FlexigridButton> buttons;
	private List<FlexigridSearchItems> searchitems;
	private String sortname;
	private String sortorder;
	private Boolean usepager;
	private String title;
	private Boolean useRp;
	private Integer rp;
	private Boolean showTableToggleBtn;
	private Boolean resizable;
	private Boolean singleSelect;
	
	
	public String getJSON() throws JSONException {
		JSONObject json = new JSONObject();

		json.putOpt("url", url);
		json.putOpt("dataType", dataType);
		if (colModel != null && !colModel.isEmpty()) {
			json.putOpt("colModel", getJSONArray(colModel));
		}
		if (buttons != null && !buttons.isEmpty()) {
			json.putOpt("buttons",  getJSONArray(buttons));
		}
		if (searchitems != null && !searchitems.isEmpty()) {
			json.putOpt("searchitems", getJSONArray(searchitems));
		}
		json.putOpt("sortname", sortname);
		json.putOpt("sortorder", sortorder);
		json.putOpt("usepager", usepager);
		json.putOpt("title", title);
		json.putOpt("useRp", useRp);
		json.putOpt("rp", rp);
		json.putOpt("showTableToggleBtn", showTableToggleBtn);
		json.putOpt("resizable", resizable);
		json.putOpt("singleSelect", singleSelect);
		
		String jsonString = json.toString(4);
		jsonString = jsonString.replaceAll("\\\"([^\\\"]*)\\\":", " $1: ");
		jsonString = jsonString.replaceAll("onpress:\\s*\\\"([^\\\"]*)\\\"", "onpress: $1");
		
		return jsonString;

	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private JSONArray getJSONArray(List list) throws JSONException {
		List<FlexigridItem> itemArray = list;
		JSONArray jsonArray = new JSONArray();
		for (FlexigridItem flexigridItem : itemArray) {
			jsonArray.put(flexigridItem.getJSONObejct());
		}
		return jsonArray;
	}



	//Getters y Setters
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDataType() {
		return dataType;
	}
	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
	public List<FlexigridColumn> getColModel() {
		return colModel;
	}
	public void setColModel(List<FlexigridColumn> colModel) {
		this.colModel = colModel;
	}
	public List<FlexigridButton> getButtons() {
		return buttons;
	}
	public void setButtons(List<FlexigridButton> buttons) {
		this.buttons = buttons;
	}
	public List<FlexigridSearchItems> getSearchitems() {
		return searchitems;
	}
	public void setSearchitems(List<FlexigridSearchItems> searchitems) {
		this.searchitems = searchitems;
	}
	public String getSortname() {
		return sortname;
	}
	public void setSortname(String sortname) {
		this.sortname = sortname;
	}
	public String getSortorder() {
		return sortorder;
	}
	public void setSortorder(String sortorder) {
		this.sortorder = sortorder;
	}
	public Boolean getUsepager() {
		return usepager;
	}
	public void setUsepager(Boolean usepager) {
		this.usepager = usepager;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Boolean getUseRp() {
		return useRp;
	}
	public void setUseRp(Boolean useRp) {
		this.useRp = useRp;
	}
	public Integer getRp() {
		return rp;
	}
	public void setRp(Integer rp) {
		this.rp = rp;
	}
	public Boolean getShowTableToggleBtn() {
		return showTableToggleBtn;
	}
	public void setShowTableToggleBtn(Boolean showTableToggleBtn) {
		this.showTableToggleBtn = showTableToggleBtn;
	}
	public Boolean getResizable() {
		return resizable;
	}
	public void setResizable(Boolean resizable) {
		this.resizable = resizable;
	}
	public Boolean getSingleSelect() {
		return singleSelect;
	}
	public void setSingleSelect(Boolean singleSelect) {
		this.singleSelect = singleSelect;
	}
	
	
//	{
//        url: /*[[ ${serviceUrl} ]]*/'',
//        dataType: 'json',
//        colModel : [
//                {display: 'ID', name : 'id', width : 31, sortable : false, align: 'left'},
//                ...
//        ],
//        buttons : [
//                {name: 'Modificar', bclass: 'edit', onpress : modifyCenter},
//                ...
//        ],
//        searchitems : [
//                {display: 'Nombre', name : 'name', isdefault: true},
//                ...
//        ],
//        sortname: "name",
//        sortorder: "asc",
//        usepager: true,
//        title: "Centros RAEE",
//        useRp: true,
//        rp: 10,
//        showTableToggleBtn: false,
//        resizable: false,
//        singleSelect: true
//   }
	
	
}
